package com.zm.demo.core.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static java.lang.System.out;

/**
 * @author zoum
 * @create 2020/1/10 8:16
 */
public class IsSymmetric1 {
    Map<Integer, List<Integer>> map = new HashMap<>();

    public boolean isSymmetric(TreeNode root) {
        if (root == null){
            return true;
        }

        if (root.right == null && root.left ==null){
            return true;
        }
        if (root.right==null && root.left != null){
            return false;
        }
        if (root.right!=null && root.left == null){
            return false;
        }

        return calc(root.left, root.right);
    }

    public boolean calc(TreeNode left, TreeNode right) {
        if ((left != null && right == null)
                || (left == null && right != null)){
            return false;
        }

        if (left != null && right != null){
            if (left.val != right.val){
                return false;
            }else{
                calc(left.left, right.right);
                calc(left.right, right.left);
            }
        }
        return true;
    }





    public static void main(String[]args){
        TreeNode node3_1 = new TreeNode(3);
        TreeNode node3_2 = new TreeNode(4);

        TreeNode node2_1 = new TreeNode(2);
        node2_1.left = node3_1;
        node2_1.right = node3_2;

        TreeNode node3_3 = new TreeNode(4);
        TreeNode node3_4 = new TreeNode(3);
        TreeNode node2_2 = new TreeNode(2);
        node2_2.left = node3_3;
        node2_2.right = node3_4;

        TreeNode node1 = new TreeNode(1);
        node1.left = node2_1;
        node1.right = node2_2;
        out.println(new IsSymmetric1().isSymmetric(node1));
    }
}
